<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>IFE ECMAScript</title>
  </head>
  <body>
    <input id="dec-number" type="number" placeholder="输入一个十进制非负整数" />
    <button id="trans-btn" onclick="showResult()">转化为二进制</button>
    <p id="result">运算结果</p>
    <script>
      function dec2bin(num) {
        // 在这里实现你的转化方法，注意需要判断输入必须为一个非负整数
        if (
          num < 0 ||
          typeof num !== "number" ||
          num === Infinity ||
          isNaN(num) ||
          num % 1 !== 0
        ) {
          return "fuck u";
        }

        let collect = "";
        collect += num % 2;
        num = Math.floor(num / 2);
        while (num != 0) {
          collect += num % 2;
          num = Math.floor(num / 2);
        }
        return collect.split("").reverse().join("");
      }
      // 实现当点击转化按钮时，将输入的十进制数字转化为二进制，并显示在result的p标签内
      function showResult() {
        let num = Number(document.querySelector("#dec-number").value);
        let result = dec2bin(num);
        //let result = typeof num;
        document.querySelector("#result").innerHTML = "运算结果为：" + result;
      }
    </script>
  </body>
</html>
